/**
 * @ author: 汪孟苹
 */
define(["jquery", "bbcare", "fu/app/comm/comm"], function ($, $bb, $comm) {
	var servers = {}
	var URL = $bb.baseUrl("followup")

	/* 请求顶部和底部的统计数据 */
	servers.ajaxStatis = function (callback) {
		$bb.ajax({
			url: URL + "followup/service/patient/patientQueueStatisticsQuery",
			success: function (data) {
				if (data.success) {
					callback(data.data)
				} else {
					$bb.toastShow(data.msg ? data.msg : "请求统计数据出错！")
				}
			}
		})
	}
	/* 请求队列selectQueue */
	servers.ajaxQueue = function (callback) {
		$bb.ajax({
			url: URL + "followup/service/queue/followQueueListQuery",
			success: function (data) {
				if (data.success) {
					callback(data.data.rows)
				} else {
					$bb.toastShow(data.msg ? data.msg : "请求队列出错！")
				}
			}
		})
	}
	/**
	 * 查询医生读写权限
	 */
	// servers.getReadWriteRole = function (data) {
	// 	$bb.ajax({
	// 		url: URL + "followup/service/getReadWriteRole",
	// 		data: JSON.stringify(data),
	// 		success: function (result) {
	// 			if (result.success) {
	// 				// if (result.data.role === '0') {
	// 				// 		document.getElementById('topbar').style = 'pointer-events: none;'
	// 				// 		if (document.getElementById('toAddInfo')) {
	// 				// 			document.getElementById('toAddInfo').style = 'pointer-events: none;'
	// 				// 		}
	// 				// }
	// 				window.__config__.role = result.data.role
	// 			} else {
	// 				$bb.toastShow(result.msg ? result.msg : "查询医生读写状态失败！")
	// 			}
	// 		}
	// 	})
	// }

	/* 请求今日门诊记录 */
	servers.ajaxPatientsOperate = function (params, callback) {
		$bb.ajax({
			url: URL + "followup/service/patient/patientOperateListQuery",
			data: JSON.stringify({
				data: params
			}),
			success: function (data) {
				if (data.success) {
					callback(data.data.rows)
				} else {
					$bb.toastShow(data.msg ? data.msg : "请求今日门诊记录出错！")
				}
			}
		})
	}


	/* 请求窗口期随访患者 */
	servers.ajaxPatientsQueue = function (params, callback) {
		return $bb.ajax({
			url: URL + "followup/service/patient/patientQueueListQuery",
			data: JSON.stringify({
				data: params
			}),
			success: function (data) {
				if (data.success) {
					callback(data.data, params)
				} else {
					$bb.toastShow(data.msg ? data.msg : "请求窗口期随访患者出错！")
				}
				$("#patientOperateListQuery").css({ "height": $("#windowBabyList").height() + $(".ws-bottom-left-bottom").height() + 60 + 'px' })
			}
		})
	}
	/* 窗口期随访患者数据处理 */
	servers.babyListData = function (list, rows) {
		var arr = []
		for (var i = 0; i < list.length; i++) {
			arr[i] = list[i]
			arr[i].gender = list[i].gender
			arr[i].percent = parseInt(list[i].completeNum / list[i].totalNum * 100)
			arr[i].width = 100 / list[i].totalNum + '%'
			if (list[i].taskStatusArr) {	// 由于taskStatusArr字段是后加的，导致之前的数据会出错，所以在这里判断一下
				arr[i].taskStatusArr = JSON.parse(list[i].taskStatusArr)
				for (var j = 0; j < arr[i].taskStatusArr.length; j++) {
					if (arr[i].taskStatusArr[j] == '0') {
						arr[i].taskStatusArr[j] = "progress-notstart"
					} else if (arr[i].taskStatusArr[j] == '1') {
						arr[i].taskStatusArr[j] = "progress-finish"
					} else if (arr[i].taskStatusArr[j] == '2') {
						arr[i].taskStatusArr[j] = "progress-overdue"
					}
				}
			}
			var hisNo = {}
			if (arr[i].hisNo1) {
				hisNo = { hisNo: arr[i].hisNo1 }
			} else if (arr[i].hisNo2) {
				hisNo = { hisNo: arr[i].hisNo2 }
			} else if (arr[i].hisNo3) {
				hisNo = { hisNo: arr[i].hisNo3 }
			}
			arr[i] = $.extend(hisNo, arr[i])
			// 年龄
			if (arr[i].birthday) {
				arr[i].age = $comm.dateDiff(arr[i].birthday, $comm.getCurTime().date).age
			} else {
				arr[i].age = ""
			}
			// 是否是早产儿
			arr[i].isPremature = false
			if (arr[i].birthday && arr[i].pregnancyDay) {
				if ($comm.dateDiff(arr[i].birthday, $comm.getCurTime().date).days < 2 * 365 && arr[i].pregnancyDay < 37 * 7) {
					arr[i].isPremature = true
				}
			}
		}
		if (arr.length < rows && arr.length > 0) {
			var remainNum = rows - arr.length
			for (var i = 0; i < remainNum; i++) {
				arr.push({})
			}
		}
		return arr
	}
	/* 姓数据处理 */
	servers.firstNameData = function (list) {
		var arr = []
		if (list.indexOf(",") > -1) {
			var arrs = list.split(",")
		} else {
			var arrs = [list]
		}
		var Num = 8  // 自定义显示Num个按钮
		if (arrs.length < Num) {
			Num = arrs.length
		}
		for (var i = 0; i < Num; i++) {
			arr[i] = arrs[i]
		}
		return arr
	}

	return servers
})
